
import 'package:boss/main.dart';
import 'package:boss/models/takeout_order_list_data.dart' as model;
import 'package:boss/provider/takeout_process_provider.dart';
import 'package:boss/ui/page/takeoutManagement/page/takeoutList/widget/takeout_list_item.dart';
import 'package:boss/ui/widget/badge.dart';
import 'package:boss/ui/widget/custom_tab.dart';
import 'package:boss/ui/widget/e_tab.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'takeout_process_tabview.dart';
import 'takeout_search_bar.dart';

class TakeoutProvessBrowser extends StatefulWidget {
  @override
  _TakeoutProvessBrowserState createState() => _TakeoutProvessBrowserState();
}

class _TakeoutProvessBrowserState extends State<TakeoutProvessBrowser> {
  int showIndex = 0;
  List<model.TakeoutOrderItem> searchTakeouts = [];

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        TakeoutSearchBar(
          onFocusChange: searchFocusHandle,
          onChanged: searchChangeHandle,
          onScan: scanHandle,
          isShowCancel: showIndex == 1,
          onCancel: onCancel,
        ),
        Expanded(
          child: IndexedStack(
            index: showIndex,
            children: <Widget>[
              Consumer<TakeoutProcessProvider>(
                builder: (context, provider, child) {
                  var tabs = provider.tabs;

                  return Container(
                    child: CustomTab(
                      tabs: tabs
                          .map((item) => ETab(
                                child: Container(
                                  margin: EdgeInsets.only(
                                    top: 8.w,
                                  ),
                                  child: Badge(
                                    child: Text(item.title),
                                    count: provider.getCountByStatus(item.value),
                                  ),
                                ),
                              ))
                          .toList(),
                      isScrollable: true,
                      tabViews: tabs
                          .map((item) => TakeoutProcessTabView(
                                status: item.value,
                                key: item.key,
                              ))
                          .toList(),
                      onChanged: (index) {
                        var provider = Provider.of<TakeoutProcessProvider>(
                          context,
                          listen: false,
                        );

                        provider.tabIndex = index;
                      },
                    ),
                  );
                },
              ),
              Container(
                color: Colors.white,
                child: ListView.builder(
                  itemBuilder: (context, index) {
                    var current = searchTakeouts[index];

                    return TakeoutListItem(
                      data: current,
                    );
                  },
                  itemCount: searchTakeouts.length,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  searchFocusHandle(bool isFocus) {
    if (isFocus) {
      setState(() {
        showIndex = 1;
      });
    }
  }

  scanHandle(String value) {
    try {
      print('扫码结果：$value');

//      var res = jsonDecode(value);

//      var takeOutId = res['takeOutId'];
//      var pickUpCode = res['pickUpCode'];

      var takeOutId = value;

      var provider = Provider.of<TakeoutProcessProvider>(
        context,
        listen: false,
      );
      provider.picked(takeOutId, null);
    } catch (e) {
      toast('读取取餐码失败');
    }
  }

  searchChangeHandle(String value) async {
    try {
      var res = await api.searchTakeoutList(input: value);

      if (!res.successful()) return;

      setState(() {
        searchTakeouts = res.data.items;
      });
    } catch (e) {
      toast(e.toString());
    }
  }

  onCancel() {
    setState(() {
      searchTakeouts = [];
      showIndex = 0;
    });
  }
}
